package cn.wolfcode.web.controller;


import cn.wolfcode.domain.NoticeMsg;
import cn.wolfcode.service.INoticeMsgService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.RequestPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/noticeMsg")
public class NoticeMsgController {

    @Autowired
    private INoticeMsgService noticeMsgService;
    @Autowired
    private INoticeService noticeService;


    @RequestMapping("/list")
    public String list(Model model,Long noticeId,HttpSession session){
        model.addAttribute("noticeMsg",noticeMsgService.get(noticeId));
        //读取后 , 修改关系表状态
        noticeService.updateEmpReadStatus(session,noticeId);
        return "noticeMsg/show";
    }


    @RequestPermission(name="通知内容删除",expression="noticeMsg:delete")
    @RequestMapping("/delete")
    public String delete(Long noticeMsgId,Long noticeId) {
        noticeMsgService.delete(noticeMsgId);
        noticeService.delete(noticeId);
        return "redirect:/notice/list";
    }


    @RequestPermission(name="通知内容新增/编辑",expression="noticeMsg:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(NoticeMsg noticeMsg, HttpSession session) {
        if (noticeMsg.getId() == null) {
            noticeMsgService.save(noticeMsg,session);
        } else {
            noticeMsgService.update(noticeMsg);
        }
        return "redirect:/notice/list";
    }


    @RequestPermission(name="通知内容新增/编辑",expression="noticeMsg:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("noticeMsg",noticeMsgService.get(id));
        }
        return "noticeMsg/input";
    }
}
